/* eslint no-constant-condition: ["error", { "checkLoops": false }] */
/**
 *
 * Copyright 2016-present scrm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import { put, take, call, fork } from 'redux-saga/effects';

import * as types from '../constants/ActionTypes';
import ToastUtil from '../utils/ToastUtil';
import RequestXhrUtil from '../utils/RequestXhrUtil';
import { UA } from '../utils/AttachParamsUtil';
import { SCRM_API } from '../constants/Urls';
import { 
  fetchUser,  
  receiveUser,
  fetchUserSites,
  receiveUserSites,
  startLogin,
  receiveLoginData
} from '../actions/user';


export function* requestUser(
  token
) {
  try {
    yield put(fetchUser());
    const userResponse = yield call(
      RequestXhrUtil.request,
      `${SCRM_API}/users/${token}`,
      'get'
    );
    yield put(receiveUser(
      userResponse.results
    ));
    const errorMessage = userResponse.errmsg;
    if (userResponse.errcode) {
      yield ToastUtil.showShort(errorMessage);
    }
  } catch (error) { 
    console.log(error, 'error')
    yield put(receiveUser(null));
    ToastUtil.showShort('网络发生错误，请重试');
  }
}


export function* requestLogin(
  loginData
) {
  try {
    yield put(startLogin());
    const userResponse = yield call(
      RequestXhrUtil.request,
      `${SCRM_API}/login`,
      'post',
      UA(loginData)
    );
    const errorMessage = userResponse.errmsg;
    if (userResponse.errcode) {
      yield put(receiveLoginData(null, errorMessage));
    } else {
      yield put(receiveLoginData(
        userResponse.results
      ));
    }
  } catch (error) { 
    console.log(error, 'error')
    yield put(receiveLoginData(null, '网络发生错误，请重试'));
  }
}

export function* requestUserSites(
  username
) {
  try {
    yield put(fetchUserSites());
    const userSitesResponse = yield call(
      RequestXhrUtil.request,
      `${SCRM_API}/user_sites?username=${username}`,
      'get'
    );
    console.log('userSitesResponse', userSitesResponse)
    yield put(receiveUserSites(
      userSitesResponse.results
    ));
    const errorMessage = userSitesResponse.errmsg;
    if (userSitesResponse.errcode) {
      yield ToastUtil.showShort(errorMessage);
    }
  } catch (error) { 
    console.log(error, 'error')
    yield put(receiveUserSites(null));
    ToastUtil.showShort('网络发生错误，请重试');
  }
}

export function* watchRequestUser() {
  while (true) {
    const {
      token
    } = yield take(types.REQUEST_USER);
    yield fork(
      requestUser,
      token
    );
  }
}

export function* watchRequestLogin() {
  while (true) {
    const {
      loginData
    } = yield take(types.REQUEST_LOGIN);
    yield fork(
      requestLogin,
      loginData
    );
  }
}


export function* watchRequestUserSites() {
  while (true) {
    const {
      username
    } = yield take(types.REQUEST_USER_SITES);
    yield fork(
      requestUserSites,
      username
    );
  }
}
